home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3533 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.2 KB  |  93 lines

  1. Path: info.spt.net.cn!usenet
  2. From: txwang@public.sta.net.cn (Wang TianXing)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Overloading conversion
  5. Date: Wed, 24 Jan 1996 18:00:01 GMT
  6. Organization: No Organization
  7. Distribution: world
  8. Message-ID: <4e5r4t$41f@info.sta.net.cn>
  9. References: <4d934v$10hs@ds2.acs.ucalgary.ca> <4e1a0v$dvh@hpbab.wv>
  10. NNTP-Posting-Host: ts2-10.sta.net.cn
  11. X-Newsreader: Forte Free Agent 1.0.82
  12.  
  13. bloom@wv.mentorg.com (Scott Aron Bloom) wrote:
  14.  
  15. | In article <4d934v$10hs@ds2.acs.ucalgary.ca>, hdang@acs3.acs.ucalgary.ca (Hanna Dang) writes:
  16. | |> Is there a way to overload functions such that certain
  17. | |> functions are called depending on the context the object are
  18. | |> used. For example,
  19. | |> 
  20. | |> class T {
  21. | |>   public:
  22. | |>     int& int_value() { modified = TRUE; return value;}
  23. | |>     int int_value() {return value;}
  24. | |>  private:
  25. | |>     int value;
  26. | |>     int modified;
  27. | |> }
  28. | |> 
  29. | |> 
  30. | |> ...
  31. | |> 
  32. | |> T a;
  33. | |> int test = a + 6 // int int_value should be called;
  34. | |> a = 20 // int& int_value should be called.
  35. | |> 
  36. | |> However, this does not work because in both case int& int_value
  37. | |> is called.  Is there another way to find out if value is changed
  38. | |> or it's just inspected.
  39. | |> 
  40.  
  41.  
  42. | To tell you the truth, I am kinda shocked that class T compiled 
  43. | at all.
  44. Yes, it should not be compiled.
  45.  
  46. | And this is what i would expect.  Remember, return values from functions in 
  47. | C/C++ are not required to be used.  Therefor you can not overload in C++
  48. | based off of the return type.
  49.  
  50. | What you really want is something like PASCAL has, the procedure/function
  51. | difference.
  52.  
  53. | While I have tried to figure out ways to do what you want, I usually 
  54. | give up.  The method I sometimes use is having a member function T::value().
  55.  
  56. | So for your code you would use
  57.  
  58. | T a;
  59. | int test = a.value + 6;
  60. | a = 20;
  61.  
  62.  
  63. | Sorry to be the bearer of bad news...
  64.  
  65. But, try this, please:
  66.  
  67. class T {
  68. public:
  69.     T( int v = 0 ) : value(v), modified(0) {}
  70.  
  71.     operator int() { return value; }
  72.     int operator =( int v ) { modified = 1; return value = v; }
  73.  
  74. private:
  75.     int value;
  76.     int modified;
  77. };
  78.  
  79. int main()
  80. {
  81.     T a(30);
  82.  
  83.     int test = a + 6;    // int() should be called
  84.     a = 20;            // =(int) should be called
  85.  
  86.     return 0;
  87. }
  88.  
  89. ---
  90. Wang TianXing
  91.  
  92.  
  93.